package cn.tedu;

import javax.swing.plaf.synth.SynthOptionPaneUI;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Scanner;

public class Demo04 {
    public static void main(String[] args) {
        Scanner sc= new Scanner(System.in);
        System.out.println("请输入用户名");
        String username = sc.nextLine();
        System.out.println("请输入密码");
        String password = sc.nextLine();
        try (Connection conn = DBUtils.getConn()){
            String sql = "select password from user where username=?";
            PreparedStatement ps = conn.prepareStatement(sql);
            ps.setString(1,username);
            ResultSet rs = ps.executeQuery();
            if (rs.next()){//查询到了密码
                String pw = rs.getString(1);//得到查询到的密码
                if (pw.equals(password)){//pw数据库里面的 password用户输入的
                    System.out.println("登录成功！");
                }else {
                    System.out.println("密码错误！");
                }
            }else {//代表没查询到
                System.out.println("用户名不存在！");
            }
        }catch (SQLException throwables){
            throwables.printStackTrace();
        }
    }
}
